package com.gywj.race.controller.api;

import com.google.common.collect.Lists;
import com.gywj.race.config.result.AjaxResultHandler;
import com.gywj.race.entity.EventInformation;
import com.gywj.race.entity.Organizer;
import com.gywj.race.service.EventService;
import com.gywj.race.util.FreemarkerUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import static com.gywj.race.util.ZXcodeUtil.getQRCodeImge;

@RestController
public class CreatePartPageController {

    @Resource
    private EventService eventService;

    @Value("${spring.application.origin_addr}")
    private String origin_addr;

    @GetMapping("/generate/part/page/{eventId}")
    public Map<String, Object> createPage1(HttpServletRequest request, HttpServletResponse response, @PathVariable("eventId") Integer eventId) throws Exception {
        try {
            // 设置地址
            //以classpath下面的static目录作为静态页面的存储目录，同时命名生成的静态html文件名称
            String path = this.getClass().getResource("/").toURI().getPath() + "static/event" + eventId;
            EventInformation eventInformation = eventService.getEventInformationById(eventId);


            String fileName = "/race-index.html";
            String templateName = "race-index.ftl";
            String templatePath = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map = new HashMap<>();
            map.put("eventId", eventId);
            map.put("eventName", eventInformation.getName());
            map.put("pic", eventInformation.getPic());
            map.put("raceBeginTime", DateFormatUtils.format(eventInformation.getRaceBeginTime(),"yyyy-MM-dd HH:mm:ss"));
            map.put("raceEndTime", DateFormatUtils.format(eventInformation.getRaceEndTime(),"yyyy-MM-dd HH:mm:ss"));
            map.put("place", eventInformation.getPlace());
            Integer organizerId = eventInformation.getOrganizerId();
            Organizer organizerById = eventService.getOrganizerById(organizerId);
            map.put("organizerName", organizerById.getIntroduction());
            map.put("host", eventInformation.getHost());
            map.put("executive", eventInformation.getExecutive());
            map.put("introduction", eventInformation.getIntroduction());
            map.put("detail", eventInformation.getDetail());


            FreemarkerUtils.geneFileStr(path, fileName, templatePath, map, templateName);
            SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            String fileName1 = "/part1.html";
            String templateName1 = "part1.ftl";
            String templatePath1 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map1 = new HashMap<>();
            map1.put("eventId", eventId);
            map1.put("eventName", eventInformation.getName());
            map1.put("pic", eventInformation.getPic());
            FreemarkerUtils.geneFileStr(path, fileName1, templatePath1, map1, templateName1);

            String fileName2 = "/part2.html";
            String templateName2 = "part2.ftl";
            String templatePath2 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map2 = new HashMap<>();
            map2.put("eventId", eventId);
            map2.put("eventName", eventInformation.getName());
            FreemarkerUtils.geneFileStr(path, fileName2, templatePath2, map2, templateName2);

            String fileName3 = "/success.html";
            String templateName3 = "success.ftl";
            String templatePath3 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map3 = new HashMap<>();
            FreemarkerUtils.geneFileStr(path, fileName3, templatePath3, map3, templateName3);

            String fileName4 = "/failed.html";
            String templateName4 = "failed.ftl";
            String templatePath4 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map4 = new HashMap<>();
            FreemarkerUtils.geneFileStr(path, fileName4, templatePath4, map4, templateName4);

            String fileName5 = "/upload.html";
            String templateName5 = "upload.ftl";
            String templatePath5 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map5 = new HashMap<>();
            map5.put("eventId", eventId);
            map5.put("eventName", eventInformation.getName());
            map5.put("eventBeginTime", dataFormat.format(eventInformation.getRaceBeginTime()));
            FreemarkerUtils.geneFileStr(path, fileName5, templatePath5, map5, templateName5);


            String fileName6 = "/numberbook.html";
            String templateName6 = "numberbook.ftl";
            String templatePath6 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map6 = new HashMap<>();
            map6.put("eventId", eventId);
            map6.put("eventName", eventInformation.getName());
            map6.put("place", eventInformation.getPlace());
            map6.put("organizerName", organizerById.getIntroduction());
            map6.put("host", eventInformation.getHost());
            map6.put("guide", eventInformation.getGuide());
            ArrayList<Object> execList = Lists.newArrayList();
            Collections.addAll(execList,eventInformation.getExecutive().split("、"));
            for (int i = 0; i < execList.size(); i++) {
                map6.put("executive"+(i+1), execList.get(i));
            }
            FreemarkerUtils.geneFileStr(path, fileName6, templatePath6, map6, templateName6);


            String fileName7 = "/certificate.html";
            String templateName7 = "certificate.ftl";
            String templatePath7 = this.getClass().getResource("/").toURI().getPath() + "templates/";
            Map<String, Object> map7 = new HashMap<>();
            map7.put("eventId", eventId);
            map7.put("eventName", eventInformation.getName());
            map7.put("place", eventInformation.getPlace());
            map7.put("guide", eventInformation.getGuide());
            map7.put("organizerName", organizerById.getIntroduction());
            map7.put("host", eventInformation.getHost());
            for (int i = 0; i < execList.size(); i++) {
                map7.put("executive"+(i+1), execList.get(i));
            }
            map7.put("eventBeginTime", dataFormat.format(eventInformation.getRaceBeginTime()));
            FreemarkerUtils.geneFileStr(path, fileName7, templatePath7, map7, templateName7);



            eventInformation.setTarget("/event" + eventId + "/part1.html");
            eventService.updateInformation(eventInformation);
            getQRCodeImge(origin_addr + "/event" + eventId + "/race-index.html", 500, 500, path + "/qrcode.png");
            return AjaxResultHandler.Success();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResultHandler.Failed(e.getMessage());
        }
    }

}
